using System.Collections.Generic;
using System.Web;
using AddonNice.Core;
using AddonNice.Settings;
using AddonNice.Configuration;
using AddonNice.Settings.Cache;

namespace AddonNice.UI.WebControls
{
	/// <summary>
	/// A PortalModuleControl that supports CustomUserSettings for authenticated users.
    /// Users can specify settings for the module instance that will apply only to them when they interact with the module.
	/// </summary>
	public class PortalModuleControlCustom : PortalModuleControl
	{
        // provide a custom SettingsDictionary  that will store user-specific settings for this instance
		// of the module.  
        protected ModuleSettingsDictionary _customUserSettings;

		public bool HasCustomizeableSettings 
		{
			get
			{
				return CustomizedUserSettings.Count > 0;
			}
		}


        public ModuleSettingsDictionary CustomizedUserSettings
		{
			get
			{
				if (_customUserSettings != null)
				{
					return _customUserSettings;
				}
				else
				{

                    ModuleSettingsDictionary tempSettings = new ModuleSettingsDictionary();
					CurrentCache.Remove(Key.ModuleSettings(this.ModuleID));
					foreach (string str in Settings.Keys)
					{
                        SettingItem _default = Settings[str];
						if (_default.Group == SettingItemGroup.CUSTOM_USER_SETTINGS)  //It's one we want to customize
						{
							tempSettings.Add(str, _default);	//insert the 'default' value
						}
					}

					//Now, replace the default settings with the custom settings for this user from the database.
                    return tempSettings.LoadModuleUserSettings(ModuleBaseConfiguration.ModuleID, PortalSettings.CurrentIdentity.CrmId);
				}
			}
		}


		#region "Customize" Button Implementation
		private ModuleButton customizeButton;
		/// <summary>
		/// Module Properties button
		/// </summary>
		public ModuleButton CustomizeButton
		{
			get
			{
				if ( customizeButton == null && HttpContext.Current != null )
				{
					// check authority
					if ( this.HasCustomizeableSettings && PortalSettings.CurrentIdentity.IsAuthenticated)
					{
						// create the button
						customizeButton                 = new ModuleButton();
						customizeButton.Group           = ModuleButton.ButtonGroup.Admin;
						customizeButton.EnglishName     = "Customize";
						customizeButton.TranslationKey  = "CUSTOMIZE";
						customizeButton.ImageSkin       = "Buttons_Properties";
						if(this.PropertiesUrl.IndexOf("?") >= 0) //Do not change if  the querystring is present (shortcut patch)
							//if ( this.ModuleID != this.OriginalModuleID ) // shortcut
							customizeButton.HRef        = PropertiesUrl;
						else
							customizeButton.HRef        = HttpUrlBuilder.BuildUrlWithCustomAttributes(Config.ModulesPath+"/"+Config.ModuleCustomPropertyPage, PageID, "mID=" + ModuleID.ToString());
						customizeButton.Target          = PropertiesTarget;
						customizeButton.RenderAs        = ButtonsRenderAs;
					}
				}
				return customizeButton;
			}
		}

		protected override void BuildButtonLists()
		{
			if ( CustomizeButton != null ) 
				ButtonListAdmRight.Add(CustomizeButton);

			base.BuildButtonLists ();
		}

		#endregion

	}

}
